package gui;

import com.thirdnf.ResourceScheduler.Appointment;
import com.thirdnf.ResourceScheduler.components.BasicAppointmentComponent;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


/**
 * Represente la composition d'un evenement pour l'affichage
 * @author Stanislas KOBAK / Celine BARILLERE
 *
 */
@SuppressWarnings("serial")
public class AppointmentComponent extends BasicAppointmentComponent implements MouseListener
{
    private AppointmentListener appointmentListener = null; // Le listener pour les clics

    private final JPopupMenu popupMenu;

    /**
     * Constructeur qui compose un evenement
     * @param appointment L'evenement a composer
     */
    public AppointmentComponent(Appointment appointment)
    {
        super(appointment);
        addMouseListener(this); // Permet a cette instance de reagir aux clics de souris

        popupMenu = new JPopupMenu();
        
        // Pour editer un evenement
        JMenuItem editItem = new JMenuItem("Edit");
        editItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (appointmentListener != null) { appointmentListener.handleEdit(_appointment); }
            }
        });
        popupMenu.add(editItem);

        // Pour supprimer un evenement
        JMenuItem deleteItem = new JMenuItem("Delete");
        deleteItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (appointmentListener != null) { appointmentListener.handleDelete(_appointment); }
            }
        });
        popupMenu.add(deleteItem);
    }

    public void setAppointmentListener(AppointmentListener appointmentListener)
    {
        this.appointmentListener = appointmentListener;
    }


    @Override
    public void mouseClicked(MouseEvent e)
    {
        if (appointmentListener != null) {
            appointmentListener.handleClick(_appointment, e.getClickCount());
        }
    }


    @Override
    public void mousePressed(MouseEvent e)
    {
        maybeShowPopup(e);
    }


    @Override
    public void mouseReleased(MouseEvent e)
    {
        maybeShowPopup(e);
    }


    private void maybeShowPopup(MouseEvent e)
    {
        if (e.isPopupTrigger()) {
            popupMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
